#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED

#include <iostream>
#include <gtkmm/main.h>
#include <gtkmm.h>
#include <SDL/SDL.h>

#include "MyDrawingArea.h"
#include "SpriteArea.h"
#include "Misc.h"
#include "ImageList.h"
#include "CurrentContext.h"
#include "Layer_List.h"
#include "Debug.h"
#include "LayerListUI.h"
#include "Dumper.h"
#include "ID_Manager.h"

using namespace std;

class Sprite
{
    public:
    uint m_image_id;
    uint m_sprite_id;
    SDL_Rect m_rect;
    bool m_visible;
    bool m_solid;
};

struct ImageData
{
 int imageId;
 string path;
};

struct SpriteData
{
    int spriteId;
    SDL_Rect rect;
};

typedef  map<string, ImageData> MAP_IID_IMAGEDATA;
typedef  map<string, SpriteData> MAP_SID_SPRITEDATA;
typedef  map<string, map<string, SpriteData> > MAP_IID_MAP_SID_SPRITEDATA;

typedef  pair<string, ImageData> PAIR_IID_IMAGEDATA;
typedef  pair<string, SpriteData> PAIR_SID_SPRITEDATA;
typedef  pair<string, map<string, SpriteData> > PAIR_IID_MAP_SID_SPRITEDATA;

class MainWindow : public Gtk::Window
{
    Gtk::Button m_button;
    Gtk::HBox m_chbClientArea;
    Gtk::VBox m_cvbLeftPan;
    Gtk::VBox m_cvbRightPan;
    Gtk::HBox m_chbDataControl;
    Gtk::ComboBox m_ccbImages;
    Gtk::ComboBox m_ccbSprites;
//    Gtk::Button m_cbAddImage;
//    Gtk::Button m_cbAddSprite;
    Gtk::CheckButton m_ccbSpriteVisible;
    Gtk::CheckButton m_ccbSpriteSolid;
    Gtk::HBox m_chbSpriteInstaceAttibutBox;
    Gtk::Button m_cbAddSpriteToScene;
//    Gtk::Button m_cbDump;

    Gtk::VBox m_cvbWindowArea;
    Gtk::VBox m_cvbToolbarBox;

    Gtk::Toolbar m_ctbStdToolBar;
    Gtk::ToolButton m_tool_new;
    Gtk::ToolButton m_tool_open;
    Gtk::ToolButton m_tool_save;

    Gtk::Toolbar m_ctbImageToolBar;
    Gtk::Label m_clImageLabel;
    Gtk::ToolItem m_tool_image_label;
    Gtk::ToolItem m_tool_image_combo;
    Gtk::ToolButton m_tool_add_image;
    Gtk::ToolButton m_tool_remove_image;

    Gtk::Toolbar m_ctbSpriteToolBar;
    Gtk::Label m_clSpriteLabel;
    Gtk::ToolItem m_tool_sprite_label;
    Gtk::ToolItem m_tool_sprite_combo;
    Gtk::ToolButton m_tool_add_sprite;
    Gtk::ToolButton m_tool_remove_sprite;
    Gtk::SeparatorToolItem m_tool_separator;

    Gtk::AspectFrame m_cafDrawingAreaAspect;
//    Gtk::ToggleButton m_ctbSelectionRect;
    MyDrawingArea m_cMyDrawingArea;
    SpriteArea m_csaSprite;

    Gtk::Frame m_cfAddToFrame;
    Gtk::VBox m_cvbAddToBox;
    Gtk::RadioButton m_crbAddToLeft, m_crbAddToRight, m_crbAddToDown, m_crbAddToUp,
        m_crbAddToTopLeft, m_crbAddToTopRight, m_crbAddToBottomLeft, m_crbAddToBottomRight;
    uint m_iAddSpriteSide;

    uint m_imageCount;
    uint m_imageIndexCounter;

    ImageList &m_imgList;
    Layer_List &m_layerList;
    CurrentContext &m_currentCtx;
    LayerListUI m_layerListUi;
    string m_strLastVisitedFolder;

    class SpriteInfoColumns : public Gtk::TreeModel::ColumnRecord {
        public:
            SpriteInfoColumns() {
                add(m_iId);
                add(m_rect);
            }

            Gtk::TreeModelColumn< uint > m_iId;
            Gtk::TreeModelColumn< SDL_Rect > m_rect;
    };

    class ImageInfoColumns : public Gtk::TreeModel::ColumnRecord {
        public:
            ImageInfoColumns() {
                add(m_iId);
                add(m_sName);
                add(m_sPath);
                add(m_refImage);
                add(m_refSpriteList);
                add(m_spriteCount);
                add(m_spriteIndexCounter);
            }

            Gtk::TreeModelColumn< uint > m_iId;
            Gtk::TreeModelColumn< Glib::ustring > m_sName;
            Gtk::TreeModelColumn< Glib::ustring > m_sPath;
            Gtk::TreeModelColumn< Glib::RefPtr<Gdk::Pixbuf> > m_refImage;
            Gtk::TreeModelColumn< Glib::RefPtr<Gtk::ListStore> > m_refSpriteList;
            Gtk::TreeModelColumn< uint > m_spriteCount;
            Gtk::TreeModelColumn< uint > m_spriteIndexCounter;
    };

    SpriteInfoColumns m_spriteListItem;
    ImageInfoColumns m_imageListItem;

    Glib::RefPtr<Gtk::ListStore> m_refImageList;
    public:
    MainWindow();
    virtual void on_addSpriteToSide_RadioButton_clicked();
    virtual void on_addImage_button_clicked();
    virtual void on_addSprite_button_clicked();
    virtual void on_addSpriteToScene_button_clicked();
//    virtual void on_selectRect_button_clicked();
    virtual void on_imageCombo_changed();
    virtual void on_spriteCombo_changed();
    virtual void on_dump();
    virtual void on_clear();
    void addSprite(const SDL_Rect &rect);
    void addImage(string filename);
    void addSpriteToLayer(uint imgId, uint spriteId, SDL_Rect rect, bool visible, bool solid);
    LayerListUI &get_layerlistUI();
    virtual void on_open();
    void clear_image_data();
};

inline LayerListUI &MainWindow::get_layerlistUI()
{
    return m_layerListUi;
}

#endif // MAINWINDOW_H_INCLUDED
